#include <fcntl.h>
#include <sys/stat.h>

#include "../lib/tlpi_hdr.h"

#ifndef BUF_SIZE
#define BUF_SIZE 1024
#endif

int main() {
    char argv1[48], argv2[48];
    scanf("%s", argv1);
    scanf("%s", argv2);

    int inputFd, outputFd, openFlags;
    mode_t filePerms;
    ssize_t numRead;
    char buf[BUF_SIZE];

    inputFd = open(argv1, O_RDONLY);
    if (inputFd == -1) {
        errExit("open file %s", argv1);
    }

    openFlags = O_CREAT | O_WRONLY | O_TRUNC;
    // 设置文件权限 rw-rw-rw
    filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    outputFd = open(argv2, openFlags, filePerms);
    if (outputFd == -1) {
        errExit("open file %s", argv2);
    }

    while ((numRead = read(inputFd, buf, BUF_SIZE)) > 0) {
        if (write(outputFd, buf, numRead) != numRead) {
            fatal("couldn't write whole buffer");
        }
    }
    if (numRead == -1) {
        errExit("read");
    }

    if (close(inputFd) == -1) {
        errExit("close input");
    }
    if (close(outputFd) == -1) {
        errExit("close output");
    }
    exit(EXIT_SUCCESS);
}